package com.zhentao.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.OrderInfoMapper;
import com.zhentao.mapper.OrderStatisticsMapper;
import com.zhentao.model.dto.order.OrderStatisticsDto;
import com.zhentao.model.entity.order.OrderInfo;
import com.zhentao.model.entity.order.OrderStatistics;
import com.zhentao.model.vo.order.OrderStatisticsVo;
import com.zhentao.service.OrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 20183
* @description 针对表【order_info(订单)】的数据库操作Service实现
* @createDate 2024-08-20 09:39:17
*/
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo>
    implements OrderInfoService{
    @Autowired
    private OrderStatisticsMapper orderStatisticsMapper;

    @Override
    public OrderStatisticsVo getOrderStatisticsData(OrderStatisticsDto orderStatisticsDto) {
        //查询统计到的结果数据
        List<OrderStatistics> orderStatisticsList = orderStatisticsMapper.selectListOs(orderStatisticsDto);

        //统计日期列表
        List<String> dateList = orderStatisticsList.stream().map(orderStatistics ->
                DateUtil.format(orderStatistics.getOrderDate(),"yyyy-MM-dd")).collect(Collectors.toList());

        //统计金额列表
        List<BigDecimal> amountList = orderStatisticsList.stream().map(OrderStatistics::getTotalAmount).collect(Collectors.toList());

        //OrderStatisticsVo对象封装响应结果数据
        OrderStatisticsVo orderStatisticsVo = new OrderStatisticsVo();
        orderStatisticsVo.setDateList(dateList);
        orderStatisticsVo.setAmountList(amountList);

        return orderStatisticsVo;
    }
}




